/*
*	@file
*	@author  Thomas Kroes <t.kroes at tudelft.nl>
*	@version 1.0
*	
*	@section LICENSE
*	
*	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*	
*	Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*	Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*	Neither the name of the TU Delft nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
*	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*	
*	Part of the SSAO code is based on the tutorial by Michael Belanec: http://www.belanecbn.sk/opengl_tutorialy/index.php?id=30
*	The bloom filter code is based on code snippets from chapter 8 in "iPhone 3D Programming: http://ofps.oreilly.com/titles/9780596804824/chadvanced.html"
*
*/

#pragma once

#include "vtkAmbientOcclusionPassDll.h"

#include <vtkImageProcessingPass.h>
#include <vtkSmartPointer.h>
#include <vtkImageData.h>

class VTK_AMBIENT_OCCLUSION_PASS_EXPORT vtkAmbientOcclusionPass : public vtkImageProcessingPass
{
public:
	static vtkAmbientOcclusionPass *New();
	vtkTypeMacro(vtkAmbientOcclusionPass, vtkImageProcessingPass);
	void PrintSelf(ostream& os, vtkIndent indent);

	//BTX
	virtual void PreRender(const vtkRenderState* RenderState);
	virtual void Render(const vtkRenderState* RenderState);
	void ReleaseGraphicsResources(vtkWindow* Window);
	//ETX

	enum 
	{
		Original = 0,
		Depth,
		Normal,
		SsaoPass,
		Composited,
		RandomSeeds
	};

	vtkGetMacro(Blur, bool);
	vtkSetMacro(Blur, bool);
	
	vtkGetMacro(RenderMode, int);
	void SetRenderMode(int RenderMode);

	vtkGetMacro(KernelRadius, float);
	void SetKernelRadius(float KernelRadius);

	vtkGetMacro(Enabled, bool);
	vtkSetMacro(Enabled, bool);

protected:
	vtkAmbientOcclusionPass();
	virtual ~vtkAmbientOcclusionPass();
	
	//BTX
	vtkSmartPointer<vtkFrameBufferObject>	FrameBufferObject;			// Frame buffer object
	vtkSmartPointer<vtkTextureObject>		DelegateTexture;			// Delegate texture
	vtkSmartPointer<vtkTextureObject>		DepthTexture;				// depth buffer
	vtkSmartPointer<vtkTextureObject>		NormalTexture;				// Normal buffer
	vtkSmartPointer<vtkTextureObject>		RandomTexture;				// Random buffer
	vtkSmartPointer<vtkTextureObject>		SsaoTexture;				// Screen space ambient occlusion buffer
	vtkSmartPointer<vtkTextureObject>		SsaoFilterTexture;			// Random seeds buffer
	vtkSmartPointer<vtkShaderProgram2>		PreprocessShader;			// Normal shader
	vtkSmartPointer<vtkShaderProgram2>		SsaoShader;					// Screen space ambient occlusion shader
	vtkSmartPointer<vtkShaderProgram2>		FilterHShader;				// Horizontal filter shader
	vtkSmartPointer<vtkShaderProgram2>		FilterVShader;				// Vertical filter shader
	vtkSmartPointer<vtkShaderProgram2>		CompositingShader;			// Compositing shader
	int										WindowSize[2];				// Last render size
	bool									Supported;					// Whether this rendering pass is supported or not
	bool									SupportChecked;				// Whether support for this rendering pass is probed or not
	bool									Blur;						// Whether to blur the screen space ambient occlusion					
	int										RenderMode;					// Type of rendering
	float									KernelRadius;				// Screen space ambient occlusion radius
	bool									Enabled;					// TODO: Write description
	int										RandomSize[2];				// Size of the random unit vector texture
	int										KernelWidth;				// Width/height of the kernel
	//ETX

private:
	vtkAmbientOcclusionPass(const vtkAmbientOcclusionPass&);		// Not implemented
	void operator=(const vtkAmbientOcclusionPass&);					// Not implemented

	void CreateNormalShader();
	void CreateSsaoShader();
	void CreateHorizontalFilterShader();
	void CreateVerticalFilterShader();
	void CreateCompositingShader();
	bool Initialize(const vtkRenderState* RenderState);
	void UpdateShaders(const vtkRenderState* RenderState);
	void ComputeSsao(const vtkRenderState* RenderState);
	void GaussianFilterSsao(const vtkRenderState* RenderState);
	void BilateralFilterSsao(const vtkRenderState* RenderState);
	void CompositeSsao(const vtkRenderState* RenderState);

	vtkSmartPointer<vtkImageData>			RandomUnitVectors;			// Compositing shader
	vtkSmartPointer<vtkImageData>			SampleVectors;				// Compositing shader

	void CreateRandomUnitVectors(const vtkRenderState* RenderState);
	void CreateSampleVectors(const vtkRenderState* RenderState);
};